package com.zhousc.springcloud;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@Slf4j
public class Controller {

    @Autowired
    private LoadBalancerClient client;

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("hello")
    public String hello(){
        ServiceInstance instance = client.choose("eureka-client");
        if (instance == null){
            return "NO avaliable instances";
        }
        String targe = String.format("http://%s:%s/sayHello",instance.getHost(),instance.getPort());
        log.info("url is {}",targe);
        return restTemplate.getForObject(targe, String.class);
    }
}
